package com.bright.tactiviti.process.start;

import org.activiti.engine.RuntimeService;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.runtime.ProcessInstanceQuery;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.List;

/**
 * 实例流程笔记
 */
@SpringBootTest
public class ProcessInstanceNote {
    //
    @Autowired
    private RuntimeService runtimeService;

    /**
     * 启动流程  使用 流程定义 ID
     *
     * 操作对应数据表
     *
     * ACT_RU_EXECUTION
     * ACT_RU_TASK
     * ACT_RU_VARIABLE
     * ACT_RU_IDENTITYLINK
     */

    @Test
    public  void startProcessByDefinitionId(){
        //定义流程ID
        String processDefinitionID = "LeaveProcess:1:4";
        //启动流程
        ProcessInstance processInstance = runtimeService.startProcessInstanceById(processDefinitionID);
        System.out.println("流程启动成功，流程实例ID：" + processInstance.getProcessInstanceId());
    }

    /**
     * 使用流程key启动流程
     */
    @Test
    public void startProcessByKey(){
        //定义key
        //String processDefinitionKey = "LeaveProcess";
//        String processDefinitionKey = "groupProcess";
        String processDefinitionKey = "fileProcess"; //启动报错，没有变量
        //启动流程
        ProcessInstance processInstance = runtimeService.startProcessInstanceByKey(processDefinitionKey);
        System.out.println("流程启动成功，流程实例ID： " + processInstance.getProcessInstanceId());
    }
    /**
     * 查询流程实例
     * 操作对应数据表
     * ACT_RU_EXECUTION
     */

    @Test
    public void processInstanceQuery(){
        //获取 流程实例 检查器
        ProcessInstanceQuery processInstanceQuery = runtimeService.createProcessInstanceQuery();

        List<ProcessInstance> processInstances = processInstanceQuery
                .list();

        processInstances.forEach(processInstance -> {
            System.out.println("执行实例ID：" + processInstance.getId());
            System.out.println("流程实例ID：" + processInstance.getProcessInstanceId());
            System.out.println("流程定义ID：" + processInstance.getDeploymentId());
        });
    }
}
